<?php

Raise::load('core.helper.RaiseHelper');
Raise::load('core.helper.sorting.IRaiseComparator');

/**
 * RaiseTimeSpanComparator class
 * Comparator for RaiseTimeSpan objects
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.datetime
 * @since 1.2
 */
abstract class RaiseDateTimeComponentComparator extends RaiseHelper implements IRaiseComparator {
    
    /**
     * Compares two objects based on a particular component
     * @param RaiseDateTime $value1 First value to compare
     * @param RaiseDateTime $value2 Second value to compare
     * @param string $com Component Name e.g. year, minute
     * @return integer Return 0 if both values are equal, 1 if $date2 > $date1 and -1 if $date2 < $date1
     */
    private function compareComponent($value1, $value2, $com){
        $time2com = $value2->$com();
        $time1com = $value1->$com();
        $ret = 0;
        if($time2com > $time1com){
            $ret = 1;
        }
        if($time2com < $time1com){
            $ret = -1;
        }       
        return $ret;
    }
    
    /**
     * Compare between two DateTime objects based on their components
     * @param object $o1
     * @param object $o2
     * @param array|RaiseCollection $components
     * @return integer
     */
    protected function compareComponents($o1, $o2, $components) {
        $componentComp = 0;
        foreach($components as $comp){
            $componentComp = $this->compareComponent($o1, $o2, $comp);
            if($componentComp != 0){
                break;
            }
        }
        return $componentComp;
    }
    
}